package Tools;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class JLabelStyle extends JLabel implements MouseListener {
	private static JLabelStyle jls;// 通用样式监听
	public static final int MOUSE_ENTERED = 1;// 鼠标进入样式
	public static final int MOUSE_EXITED = 2;// 鼠标离开样式
	public static final int MOUSE_PRESSED = 3;// 鼠标按下样式
	public static final int MOUSE_CLICKED = 3;// 鼠标按下样式
	private static Font font = new Font("Default", 0, 12);
	private static Font bordFont = new Font("微软雅黑", 1, 12);
	private static Color def;
	public static final Cursor defCur = new Cursor(Cursor.DEFAULT_CURSOR);

	public static final Cursor handCur = new Cursor(Cursor.HAND_CURSOR);

	public static MouseListener getStyleListener() {
		if (jls == null)
			jls = new JLabelStyle(null, null, null, null);

		return jls;

	}

	/**
	 * 改变标签外观方法
	 * 
	 * @param jl
	 * @param Mtype
	 *            0 默认
	 */
	public static void jlStyle(JLabel jl, int MouseTyoe) {
		switch (MouseTyoe) {
		case MOUSE_ENTERED:
			jl.setForeground(Color.GREEN);
			jl.setCursor(handCur);// 手型光标
			break;
		case MOUSE_EXITED:
			jl.setForeground(def);
			jl.setCursor(defCur);// 默认光标
			break;
		case MOUSE_PRESSED:
			jl.setForeground(Color.red);
			jl.setCursor(handCur);// 手型光标
			break;
		}
	}

	public static void setStyle(JLabel jl) {

		setStyle(jl, false);

	}

	/**
	 * 标签风格设置
	 * 
	 * @param jl
	 */
	public static void setStyle(JLabel jlTile, boolean b) {
		if (b)
			jlTile.setFont(bordFont);
		else
			jlTile.setFont(font);
	}

	private Icon m = new ImageIcon("Skin/button1.bmp");
	private Icon en = new ImageIcon("Skin/button2.bmp");

	private Icon pr = new ImageIcon("Skin/button3.bmp");

	private Icon jin = new ImageIcon("Skin/button4.bmp");
	public JLabelStyle(Icon m, Icon e, Icon r) {
		this(m, e, r, null);
	}
	public JLabelStyle(Icon m, Icon en, Icon pr, Icon jin) {
		this.m = m;
		this.en = en;
		this.pr = pr;
		this.jin = jin;
		this.setIcon(m);
		this.addMouseListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	// --鼠标进入事件处理
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.getClass() == JLabel.class) {
			JLabel jl = (JLabel) obj;
			def = jl.getForeground();
			JLabelStyle.jlStyle(jl, JLabelStyle.MOUSE_ENTERED);

		}
		if (obj.equals(this)) {
			this.setIcon(this.en);
			setCursor(handCur);// 手型光标
		}
	}

	// --鼠标离开事件
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.getClass() == JLabel.class) {
			JLabel jl = (JLabel) obj;
			jl.setCursor(defCur);
			JLabelStyle.jlStyle(jl, JLabelStyle.MOUSE_EXITED);
		}
		if (obj.equals(this)) {
			this.setIcon(this.m);
			setCursor(defCur);// 默认光标
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.getClass() == JLabel.class) {
			JLabel jl = (JLabel) obj;
			JLabelStyle.jlStyle(jl, JLabelStyle.MOUSE_PRESSED);
		}
		if (obj.equals(this)) {
			this.setIcon(this.pr);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.getClass() == JLabel.class) {
			JLabel jl = (JLabel) obj;
			JLabelStyle.jlStyle(jl, JLabelStyle.MOUSE_ENTERED);
		}
		if (obj.equals(this)) {
			this.setIcon(this.en);
		}
	}

}
